const axios = require('axios');

async function testHttpsCallback() {
  console.log('🧪 测试HTTPS回调处理');
  
  const testCases = [
    {
      name: '无参数测试（应该重定向到错误页面）',
      url: 'https://13be2816e0e8.ngrok-free.app/lxt/shuidi-callback'
    },
    {
      name: '缺少code参数（应该重定向到错误页面）',
      url: 'https://13be2816e0e8.ngrok-free.app/lxt/shuidi-callback?type=qq'
    },
    {
      name: '模拟完整回调（会尝试获取用户信息）',
      url: 'https://13be2816e0e8.ngrok-free.app/lxt/shuidi-callback?type=qq&code=test123456'
    }
  ];

  for (const test of testCases) {
    console.log(`\n🔍 ${test.name}`);
    console.log(`🔗 URL: ${test.url}`);
    
    try {
      const response = await axios.get(test.url, {
        maxRedirects: 0,
        validateStatus: function (status) {
          return status >= 200 && status < 400;
        },
        headers: {
          'ngrok-skip-browser-warning': 'true',
          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
        }
      });
      
      console.log('状态码:', response.status);
      
      if (response.status >= 300 && response.status < 400) {
        const location = response.headers.location;
        console.log('重定向到:', location);
        
        if (location.includes('/login-error')) {
          console.log('✅ 正确重定向到错误页面');
        } else if (location.includes('/login-success')) {
          console.log('✅ 重定向到登录成功页面');
        } else if (location.includes('/register-success')) {
          console.log('✅ 重定向到注册成功页面');
        } else {
          console.log('⚠️ 重定向到未知页面');
        }
      }
      
    } catch (error) {
      if (error.response && error.response.status >= 300) {
        const location = error.response.headers.location;
        console.log('重定向到:', location);
        
        if (location.includes('/login-error')) {
          console.log('✅ 正确重定向到错误页面');
        }
      } else {
        console.log('❌ 测试失败:', error.message);
      }
    }
  }
}

async function main() {
  console.log('=== HTTPS回调测试 ===\n');
  console.log('📝 测试修复后的HTTPS回调处理');
  
  await testHttpsCallback();
  
  console.log('\n🎯 测试总结:');
  console.log('✅ 如果看到"正确重定向到错误页面"，说明修复成功');
  console.log('⚠️ 如果看到其他重定向，可能需要进一步调试');
  console.log('💡 真实第三方登录时，会携带有效的code参数');
}

main().catch(console.error); 